package v1

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"server/model/request"

	"server/model/response"
	"server/service"
	"server/utils"
)

//更新角色权限列表；角色授权
// roleid [path,method]
func UpdateCasbin(c *gin.Context) {
	roleId := c.Param("role_id")

	if roleId == "" {
		response.FailWithMessage("查询失败，角色ID不能为空", c)
	}

	var cmr request.Casbin
	_ = c.ShouldBindJSON(&cmr)
	if err := utils.Verify(cmr, utils.AuthorityIdVerify); err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := service.UpdateCasbin(roleId, cmr.CasbinInfos); err != nil {
		fmt.Printf("更新失败! err %v\n", err)
		response.FailWithMessage("更新失败", c)
	} else {
		response.OkWithMessage("更新成功", c)
	}
}

// 根据roleID获取权限列表
//[path,method]
func GetPolicyPathByRoleId(c *gin.Context) {
	roleId := c.Param("role_id")

	if roleId == "" {
		response.FailWithMessage("查询失败，角色ID不能为空", c)
	}

	paths := service.GetPolicyPathByRoleId(roleId)
	response.OkWithDetailed(response.PolicyPathResponse{Paths: paths}, "获取成功", c)
}
